/*
 * CartGUI.java
 * 
 * Created by demory on Sep 13, 2009, 5:18:15 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.fpdev.apps.cart.CartModule.ID;
import org.fpdev.apps.cart.Cartographer;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.gui.control.ControlFrameManager;
import org.fpdev.apps.cart.modules.FileOps;

/**
 *
 * @author demory
 */
public class CartGUI extends JFrame {

  private Cartographer cart_;

  //private ControlPanel controlPanel_;
  private MenuBar menuBar_;
  private ControlFrameManager cfm_;

  private Toolbar toolbar_;
  private int toolbarAction_;

  private JDesktopPane desktop_;

  private EditorFrame activeFrame_;

  private Dimension lastSize_;

  private int edFramesOpen_ = 0;

  public CartGUI(Cartographer cart) {
    super("Five Points Cartographer");

    cart_ = cart;

    toolbar_ = new Toolbar(cart, this);
    toolbarAction_ = 0;

    desktop_ = new JDesktopPane();
    desktop_.setBackground(Color.lightGray);

    cfm_ = new ControlFrameManager(cart_, this);

    menuBar_ = new MenuBar(cart, this, ((FileOps) cart.getModule(ID.FILE)).getRecentFiles());
    setJMenuBar(menuBar_);
    
    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(toolbar_, BorderLayout.NORTH);
    mainPanel.add(desktop_, BorderLayout.CENTER);

    add(mainPanel);

    pack();
    setSize(new Dimension(800, 600));
    setLocationRelativeTo(null);
    setVisible(true);
    lastSize_ = getSize();

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        Dimension newSize = CartGUI.this.getSize();
        cfm_.resized(newSize.width-lastSize_.width, newSize.height-lastSize_.height);
        lastSize_ = newSize;
      }
    });

  }

  public void editorFrameOpened() {
    edFramesOpen_++;
  }

  public void editorFrameClosed() {
    edFramesOpen_--;

    if(edFramesOpen_ == 0) {
      //System.out.println("last frame closed");
      cfm_.editorChanged(activeFrame_.getEditor(), null);
      activeFrame_ = null;
    }
  }

  public ControlFrameManager getControlFrameManager() {
    return cfm_;
  }

  public MenuBar getCartMenuBar() {
    return menuBar_;
  }

  public JDesktopPane getDesktop() {
    return desktop_;
  }

  /*public Canvas getCanvas() {
    return canvas_;
  }*/

  public void msg(String msg) {
    System.out.println("MSG: "+msg);
    cfm_.getOutputFrame().msg(msg);
  }

  public void setToolbarAction(int action) {
    toolbarAction_ = action;
  }

  public int getToolbarAction() {
    return toolbarAction_;
  }

  public void updateUndoRedo(Editor ed) {
    //Editor ed = cart_.getActiveEditor();
    //System.out.println("uUR = "+ed);
    boolean undoExists = (ed != null) ? ed.getHistory().undoActionExists() : false;
    String undoText = "Undo " + (undoExists ? ed.getHistory().undoActionName() : "");
    menuBar_.getUndoItem().setEnabled(undoExists);
    menuBar_.getUndoItem().setText(undoText);
    toolbar_.getUndoButton().setEnabled(undoExists);
    toolbar_.getUndoButton().setToolTipText(undoText);

    boolean redoExists = (ed != null) ? ed.getHistory().redoActionExists() : false;
    String redoText = "Redo " + (redoExists ? ed.getHistory().redoActionName() : "");
    menuBar_.getRedoItem().setEnabled(redoExists);
    menuBar_.getRedoItem().setText(redoText);
    toolbar_.getRedoButton().setEnabled(redoExists);
    toolbar_.getRedoButton().setToolTipText(redoText);
  }

  public void addEditor(Editor ed) {
    String title = ed.getDocument().hasActiveFile() ? ed.getDocument().getActiveFile().getName() : "Untitled-"+ed.getID();
    EditorFrame frame = new EditorFrame(cart_, ed, this, title);
    desktop_.add(frame);
    frame.setSize(400,400);
    frame.setVisible(true);
    //frame.getCanvas().repaint();
  }

  public EditorFrame getActiveEditorFrame() {
    return activeFrame_;
  }

  void setActiveEditorFrame(EditorFrame frame) {
    Editor oldEditor = activeFrame_ != null ? activeFrame_.getEditor() : null;
    activeFrame_ = frame;
    Editor newEditor = frame.getEditor();
    if(oldEditor != newEditor) {
      cfm_.editorChanged(oldEditor, newEditor);
      menuBar_.updateEdPropItems(newEditor);
    }
  }
}

